package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.CompanyAndUserV2Param;
import com.dmap.admin.service.backgroundv2.CompanyService;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * \* User: helei
 * \* Date: 02/05/18
 * \* Description:
 * \
 */
@Api(value = "我的公司管理", description = "background/我的公司管理")
//@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/myCompany/")
public class MyCompanyController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CompanyService companyService;

    @ResponseBody
    @ApiOperation(value = "修改 myCompany",
            notes = "1. 必填： id, 项目编号number, 项目名称name, 联系电话companyPhone, " +
                    " 项目地址province, city, ownedByCompanyId;")
    @RequestMapping(value = "update/myCompany", method = RequestMethod.POST)
    public BaseVo update(@RequestBody CompanyAndUserV2Param param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId()) || !StringUtils.isNumeric(param.getId())
                || Long.valueOf(param.getId()).equals(0L) || StringUtils.isBlank(param.getNumber())
                || StringUtils.isBlank(param.getName()) || StringUtils.isBlank(param.getCompanyPhone())
                || StringUtils.isBlank(param.getProvince()) || StringUtils.isBlank(param.getCity())
                || StringUtils.isBlank(param.getOwnedByCompanyId()) || !StringUtils.isNumeric(param.getOwnedByCompanyId())
                || Long.valueOf(param.getOwnedByCompanyId()).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        param.setAdministratorId(null);
        param.setAdministratorAccount(null);
        param.setAdministratorHandphone(null);
        boolean success;
        try {
            success = companyService.updateCompany(param);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            baseVo.setCode(AdminConstant.RESPONSE_COMPANY_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_COMPANY_ERROR_MSG);
            return baseVo;
        }
        if (!success) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return baseVo;
    }

}